<!DOCTYPE html>
<html lang="zh-CN">
    <head>
  <!-- 元数据 -->
  <meta charset="utf-8">
  <link rel="icon" href="/blog/images/logo.png">
  
  <title>【Java 8 新特性】时间类：LocalTime | 溪岚花的博客</title>
  
  <meta name="author" content="林家隆" />
  <meta http-equiv="Cache-Control" content="no-transform" />
  <meta http-equiv="Cache-Control" content="no-siteapp" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="robots" content="index,follow" />
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <meta name="format-detection" content="telphone=no, email=no" />
  
    <meta name="keywords" content="Java" />
  
  <meta name="description" content="LocalTime 类用来表示时间，由四个成员变量 hour 、 minute 、 second 和 nano 组成，分别表示时分秒纳秒。大部分成员方法是操作四个成员变量的过程，如查询由这四个变量组成的其他的时间分量，对四个变量进行加、减或重新赋予新值等等操作。 公共常量12345678910111213&#x2F;*** MAX：localTime对象能表示时间的最大值。* MIN：localTime对象">
<meta property="og:type" content="article">
<meta property="og:title" content="【Java 8 新特性】时间类：LocalTime">
<meta property="og:url" content="https://lin316.gitee.io/blog/2023/05/07/Java8/time/LocalTime/index.html">
<meta property="og:site_name" content="溪岚花的博客">
<meta property="og:description" content="LocalTime 类用来表示时间，由四个成员变量 hour 、 minute 、 second 和 nano 组成，分别表示时分秒纳秒。大部分成员方法是操作四个成员变量的过程，如查询由这四个变量组成的其他的时间分量，对四个变量进行加、减或重新赋予新值等等操作。 公共常量12345678910111213&#x2F;*** MAX：localTime对象能表示时间的最大值。* MIN：localTime对象">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://lin316.gitee.io/blog/blog/images/logo.png">
<meta property="article:published_time" content="2023-05-06T16:00:00.000Z">
<meta property="article:modified_time" content="2025-11-20T11:35:22.178Z">
<meta property="article:author" content="林家隆">
<meta property="article:tag" content="Java">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://lin316.gitee.io/blog/blog/images/logo.png">
  
  <!-- 站点验证相关 -->
  
    
    
    
  
  <!-- 样式表文件 -->
  <link rel="stylesheet" id="kratos-css" href="/blog/css/kratosr.min.css" media="all"></script>
  
    <link rel="stylesheet" id="darkmode-css" href="/blog/css/kr-color-dark.min.css" media="(prefers-color-scheme: dark)"></script>
    <script src="/blog/js/kr-dark.min.js"></script>
  
  
    <link rel="stylesheet" id="highlight-css" href="/blog/css/highlight/night-eighties.min.css" media="all"></script>
  
  <link rel="stylesheet" id="fontawe-css" href="/blog/vendors/font-awesome@4.7.0/css/font-awesome.min.css" media="all"></script>
  <link rel="stylesheet" id="nprogress-css" href="/blog/vendors/nprogress@0.2.0/nprogress.css" media="all"></script>
  
  
  
    <link rel="stylesheet" href="/blog/vendors/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css"></script>
  
  <!-- 不得不预先加载的一些JS文件 -->
  <script src="/blog/vendors/jquery@3.6.0/dist/jquery.min.js"></script>
  
  
  <style>
    
    
  </style>
  
<meta name="generator" content="Hexo 6.3.0"></head>


    <body class="custom-background">
        <div id="kratos-wrapper">
    <div id="kratos-page">
        <div id="kratos-header">
            <header id="kratos-desktop-topnav" class="kratos-topnav">
                <div class="container">
                    <div class="nav-header">
                        <nav id="kratos-menu-wrap">
                            <ul id="kratos-primary-menu" class="sf-menu">
                                
                                    
                                        <li>
                                            
                                                <a href="/blog/">
                                            
                                                
                                                    <i class="fa fa-home"></i>
                                                
                                                首页
                                            </a>
                                            
                                        </li>
                                    
                                        <li>
                                            
                                                <a href="/blog/archives/">
                                            
                                                
                                                    <i class="fa fa-file"></i>
                                                
                                                归档
                                            </a>
                                            
                                        </li>
                                    
                                        <li>
                                            
                                                <a href="/blog/tags/">
                                            
                                                
                                                    <i class="fa fa-tag"></i>
                                                
                                                标签
                                            </a>
                                            
                                        </li>
                                    
                                        <li>
                                            
                                                <a href="/blog/categories/">
                                            
                                                
                                                    <i class="fa fa-folder"></i>
                                                
                                                目录
                                            </a>
                                            
                                        </li>
                                    
                                        <li>
                                            
                                                <a href="/blog/search/">
                                            
                                                
                                                    <i class="fa fa-search"></i>
                                                
                                                搜索
                                            </a>
                                            
                                        </li>
                                    
                                
                            </ul>
                        </nav>
                    </div>
                </div>
            </header>
            <header id="kratos-mobile-topnav" class="kratos-topnav">
                <div class="container">
                    <div class="color-logo"><a href="/blog/">溪岚花的博客</a></div>
                    <div class="nav-toggle">
                        <a class="kratos-nav-toggle js-kratos-nav-toggle">
                            <i></i>
                        </a>
                    </div>
                </div>
            </header>
        </div>
        <div class="kratos-start kratos-hero-2">
            <!-- <div class="kratos-overlay"></div> -->
            <div class="kratos-cover kratos-cover-2 text-center">
                <div class="desc desc2 animate-box">
                    <a href="/blog/">
                        <h2 style="opacity: 0.2;">溪岚花的博客</h2> <br />
                        <span></span>
                    </a>
                </div>
            </div>
        </div>

        <div id="kratos-blog-post">
            <div class="container">
                <div id="main" class="row">
                    

        

            <section class="col-md-8">

        

            <article itemscope itemtype="https://schema.org/Article">
    
    <link itemprop="mainEntityOfPage" href="https://lin316.gitee.io/blog/2023/05/07/Java8/time/LocalTime/">
    <div class="kratos-hentry kratos-post-inner clearfix">
        <header class="kratos-entry-header">
            
                <h1 class="kratos-entry-title text-center" itemprop="name headline">【Java 8 新特性】时间类：LocalTime</h1>
            
            
            <ul class="kratos-post-meta text-center">
                <li><time datetime="2023-05-06T16:00:00.000Z" itemprop="datePublished"><i class="fa fa-calendar"></i> 2023-05-07</time></li>
                <li itemprop="author" itemscope itemtype="https://schema.org/Person">
                    <i class="fa fa-user"></i> 作者 <span itemprop="name">林家隆</span>
                </li>
                
                    <li>
                        <i class="fa fa-edit"></i> 
                        
                        
                            13.11K
                        
                        字
                    </li>
                
                
            </ul>
        </header>
        <div class="kratos-post-content">
            
            <div id="expire-alert" class="alert alert-warning hidden" role="alert">
                <div class="icon"><i class="fa fa-warning"></i></div>
                <div class="text"><p>本文最后编辑于 <time datetime="1763638522178"></time> 前，其中的内容可能需要更新。</p></div>
            </div>
            
            
            
                <div class="kratos-post-inner-toc toc-div-class" >
                    <ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%AC%E5%85%B1%E5%B8%B8%E9%87%8F"><span class="toc-number">1.</span> <span class="toc-text">公共常量</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9D%99%E6%80%81%E6%96%B9%E6%B3%95"><span class="toc-number">2.</span> <span class="toc-text">静态方法</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#from-TemporalAccessor-temporal"><span class="toc-number">2.1.</span> <span class="toc-text">from(TemporalAccessor temporal)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#now"><span class="toc-number">2.2.</span> <span class="toc-text">now()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#now-ZoneId-zone"><span class="toc-number">2.3.</span> <span class="toc-text">now(ZoneId zone)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#now-Clock-clock"><span class="toc-number">2.4.</span> <span class="toc-text">now(Clock clock)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#of-int-hour-int-minute"><span class="toc-number">2.5.</span> <span class="toc-text">of(int hour, int minute)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#of-int-hour-int-minute-int-second"><span class="toc-number">2.6.</span> <span class="toc-text">of(int hour, int minute, int second)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#of-int-hour-int-minute-int-second-int-nanoOfSecond"><span class="toc-number">2.7.</span> <span class="toc-text">of(int hour, int minute, int second, int nanoOfSecond)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#ofSecondOfDay-long-secondOfDay"><span class="toc-number">2.8.</span> <span class="toc-text">ofSecondOfDay(long secondOfDay)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#ofNanoOfDay-long-nanoOfDay"><span class="toc-number">2.9.</span> <span class="toc-text">ofNanoOfDay(long nanoOfDay)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#parse-CharSequence-text"><span class="toc-number">2.10.</span> <span class="toc-text">parse(CharSequence text)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#parse-CharSequence-text-DateTimeFormatter-formatter"><span class="toc-number">2.11.</span> <span class="toc-text">parse(CharSequence text, DateTimeFormatter formatter)</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%88%90%E5%91%98%E6%96%B9%E6%B3%95"><span class="toc-number">3.</span> <span class="toc-text">成员方法</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#atDate-LocalDate-date"><span class="toc-number">3.1.</span> <span class="toc-text">atDate(LocalDate date)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#atOffset-ZoneOffset-offset"><span class="toc-number">3.2.</span> <span class="toc-text">atOffset(ZoneOffset offset)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#compareTo-LocalTime-other"><span class="toc-number">3.3.</span> <span class="toc-text">compareTo(LocalTime other)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#isAfter-LocalTime-other"><span class="toc-number">3.4.</span> <span class="toc-text">isAfter(LocalTime other)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#isBefore-LocalTime-other"><span class="toc-number">3.5.</span> <span class="toc-text">isBefore(LocalTime other)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#format-DateTimeFormatter-formatter"><span class="toc-number">3.6.</span> <span class="toc-text">format(DateTimeFormatter formatter)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#isSupported-TemporalField-field"><span class="toc-number">3.7.</span> <span class="toc-text">isSupported(TemporalField field)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#isSupported-TemporalUnit-unit"><span class="toc-number">3.8.</span> <span class="toc-text">isSupported(TemporalUnit unit)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#get-TemporalField-field"><span class="toc-number">3.9.</span> <span class="toc-text">get(TemporalField field)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#getLong-TemporalField-field"><span class="toc-number">3.10.</span> <span class="toc-text">getLong(TemporalField field)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#getHour"><span class="toc-number">3.11.</span> <span class="toc-text">getHour()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#getMinute"><span class="toc-number">3.12.</span> <span class="toc-text">getMinute()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#getNano"><span class="toc-number">3.13.</span> <span class="toc-text">getNano()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#getSecond"><span class="toc-number">3.14.</span> <span class="toc-text">getSecond()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#minus-long-amountToSubtract-TemporalUnit-unit"><span class="toc-number">3.15.</span> <span class="toc-text">minus(long amountToSubtract, TemporalUnit unit)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#minus-TemporalAmount-amountToSubtract"><span class="toc-number">3.16.</span> <span class="toc-text">minus(TemporalAmount amountToSubtract)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#minusHours-long-hoursToSubtract"><span class="toc-number">3.17.</span> <span class="toc-text">minusHours(long hoursToSubtract)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#minusMinutes-long-minutesToSubtract"><span class="toc-number">3.18.</span> <span class="toc-text">minusMinutes(long minutesToSubtract)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#minusNanos-long-nanosToSubtract"><span class="toc-number">3.19.</span> <span class="toc-text">minusNanos(long nanosToSubtract)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#minusSeconds-long-secondsToSubtract"><span class="toc-number">3.20.</span> <span class="toc-text">minusSeconds(long secondsToSubtract)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#plus-long-amountToSubtract-TemporalUnit-unit"><span class="toc-number">3.21.</span> <span class="toc-text">plus(long amountToSubtract, TemporalUnit unit)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#plus-TemporalAmount-amountToSubtract"><span class="toc-number">3.22.</span> <span class="toc-text">plus(TemporalAmount amountToSubtract)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#plusHours-long-hoursToAdd"><span class="toc-number">3.23.</span> <span class="toc-text">plusHours(long hoursToAdd)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#plusMinutes-long-minutesToAdd"><span class="toc-number">3.24.</span> <span class="toc-text">plusMinutes(long minutesToAdd)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#plusNanos-long-nanosToAdd"><span class="toc-number">3.25.</span> <span class="toc-text">plusNanos(long nanosToAdd)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#plusSeconds-long-secondstoAdd"><span class="toc-number">3.26.</span> <span class="toc-text">plusSeconds(long secondstoAdd)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#toSecondOfDay"><span class="toc-number">3.27.</span> <span class="toc-text">toSecondOfDay()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#toNanoOfDay"><span class="toc-number">3.28.</span> <span class="toc-text">toNanoOfDay()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#query-TemporalQuery-lt-R-gt-query"><span class="toc-number">3.29.</span> <span class="toc-text">query(TemporalQuery&lt;R&gt; query)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#range-TemporalField-field"><span class="toc-number">3.30.</span> <span class="toc-text">range(TemporalField field)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#truncatedTo-TemporalUnit-unit"><span class="toc-number">3.31.</span> <span class="toc-text">truncatedTo(TemporalUnit unit)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#until-Temporal-endExclusive-TemporalUnit-unit"><span class="toc-number">3.32.</span> <span class="toc-text">until(Temporal endExclusive, TemporalUnit unit)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#with-TemporalField-field-long-newValue"><span class="toc-number">3.33.</span> <span class="toc-text">with(TemporalField field, long newValue)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#withHour-int-hour"><span class="toc-number">3.34.</span> <span class="toc-text">withHour(int hour)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#withMinute-int-minute"><span class="toc-number">3.35.</span> <span class="toc-text">withMinute(int minute)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#withNano-int-nanoOfSecond"><span class="toc-number">3.36.</span> <span class="toc-text">withNano(int nanoOfSecond)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#withSecond-int-second"><span class="toc-number">3.37.</span> <span class="toc-text">withSecond(int second)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#with-TemporalAdjuster-adjuster"><span class="toc-number">3.38.</span> <span class="toc-text">with(TemporalAdjuster adjuster)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9C%AA%E6%BC%94%E7%A4%BA%E6%96%B9%E6%B3%95"><span class="toc-number">3.39.</span> <span class="toc-text">未演示方法</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8F%82%E8%80%83%E6%96%87%E7%8C%AE"><span class="toc-number">4.</span> <span class="toc-text">参考文献</span></a></li></ol>
                </div>
            
            <hr />
            <div itemprop="articleBody"><p><code>LocalTime</code> 类用来表示时间，由四个成员变量 <code>hour</code> 、 <code>minute</code> 、 <code>second</code> 和 <code>nano</code> 组成，分别表示时分秒纳秒。大部分成员方法是操作四个成员变量的过程，如查询由这四个变量组成的其他的时间分量，对四个变量进行加、减或重新赋予新值等等操作。</p>
<h3 id="公共常量"><a href="#公共常量" class="headerlink" title="公共常量"></a>公共常量</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* MAX：localTime对象能表示时间的最大值。</span></span><br><span class="line"><span class="comment">* MIN：localTime对象能表示时间的最小值。</span></span><br><span class="line"><span class="comment">* MIDNIGHT：午夜 00：00 点</span></span><br><span class="line"><span class="comment">* NOON：中午 12：00 点</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test01</span><span class="params">()</span>&#123;</span><br><span class="line">    System.out.println(LocalTime.MAX); <span class="comment">// 输出 23:59:59.999999999</span></span><br><span class="line">    System.out.println(LocalTime.MIN); <span class="comment">// 输出 00:00</span></span><br><span class="line">    System.out.println(LocalTime.MIDNIGHT); <span class="comment">// 输出 00:00</span></span><br><span class="line">    System.out.println(LocalTime.NOON); <span class="comment">// 输出 12:00</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<br>

<h3 id="静态方法"><a href="#静态方法" class="headerlink" title="静态方法"></a>静态方法</h3><h4 id="from-TemporalAccessor-temporal"><a href="#from-TemporalAccessor-temporal" class="headerlink" title="from(TemporalAccessor temporal)"></a>from(TemporalAccessor temporal)</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* from(TemporalAccessor temporal)</span></span><br><span class="line"><span class="comment">* 作用：从其他含有时分秒数据的时间对象获取localTime对象。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test01</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="type">LocalDateTime</span> <span class="variable">dateTime01</span> <span class="operator">=</span> LocalDateTime.parse(<span class="string">&quot;2023-01-01T12:00:00&quot;</span>);</span><br><span class="line">    System.out.println(LocalTime.from(dateTime01)); <span class="comment">// 输出 12:00</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="now"><a href="#now" class="headerlink" title="now()"></a>now()</h4><h4 id="now-ZoneId-zone"><a href="#now-ZoneId-zone" class="headerlink" title="now(ZoneId zone)"></a>now(ZoneId zone)</h4><h4 id="now-Clock-clock"><a href="#now-Clock-clock" class="headerlink" title="now(Clock clock)"></a>now(Clock clock)</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* now()</span></span><br><span class="line"><span class="comment">* 作用：从操作系统获取时间数据。</span></span><br><span class="line"><span class="comment">*</span></span><br><span class="line"><span class="comment">* now(ZoneId zone)</span></span><br><span class="line"><span class="comment">* 作用：获取其他时区此刻的时间。</span></span><br><span class="line"><span class="comment">*</span></span><br><span class="line"><span class="comment">* now(Clock clock)</span></span><br><span class="line"><span class="comment">* 作用：从clock对象获取时间数据。clock对象有时区的概念，所以获取的是指定地区的时间。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test02</span><span class="params">()</span>&#123;</span><br><span class="line">    System.out.println(LocalTime.now()); <span class="comment">// 输出 15:06:58.569</span></span><br><span class="line"></span><br><span class="line">    <span class="type">ZoneId</span> <span class="variable">zoneId01</span> <span class="operator">=</span> ZoneId.of(<span class="string">&quot;America/Los_Angeles&quot;</span>);</span><br><span class="line">    System.out.println(LocalTime.now(zoneId01)); <span class="comment">// 输出 00:06:58.570</span></span><br><span class="line"></span><br><span class="line">    <span class="type">Instant</span> <span class="variable">instant01</span> <span class="operator">=</span> Instant.parse(<span class="string">&quot;2023-04-01T10:00:00Z&quot;</span>);</span><br><span class="line">    <span class="type">Clock</span> <span class="variable">fixedClock01</span> <span class="operator">=</span> Clock.fixed(instant01, zoneId01);</span><br><span class="line">    System.out.println(LocalTime.now(fixedClock01)); <span class="comment">// 输出 03:00</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="of-int-hour-int-minute"><a href="#of-int-hour-int-minute" class="headerlink" title="of(int hour, int minute)"></a>of(int hour, int minute)</h4><h4 id="of-int-hour-int-minute-int-second"><a href="#of-int-hour-int-minute-int-second" class="headerlink" title="of(int hour, int minute, int second)"></a>of(int hour, int minute, int second)</h4><h4 id="of-int-hour-int-minute-int-second-int-nanoOfSecond"><a href="#of-int-hour-int-minute-int-second-int-nanoOfSecond" class="headerlink" title="of(int hour, int minute, int second, int nanoOfSecond)"></a>of(int hour, int minute, int second, int nanoOfSecond)</h4><h4 id="ofSecondOfDay-long-secondOfDay"><a href="#ofSecondOfDay-long-secondOfDay" class="headerlink" title="ofSecondOfDay(long secondOfDay)"></a>ofSecondOfDay(long secondOfDay)</h4><h4 id="ofNanoOfDay-long-nanoOfDay"><a href="#ofNanoOfDay-long-nanoOfDay" class="headerlink" title="ofNanoOfDay(long nanoOfDay)"></a>ofNanoOfDay(long nanoOfDay)</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* of(int hour, int minute)</span></span><br><span class="line"><span class="comment">* of(int hour, int minute, int second)</span></span><br><span class="line"><span class="comment">* of(int hour, int minute, int second, int nanoOfSecond)</span></span><br><span class="line"><span class="comment">* 作用：给定时分秒纳秒数据创建localTime对象。</span></span><br><span class="line"><span class="comment">*</span></span><br><span class="line"><span class="comment">* ofSecondOfDay(long secondOfDay)</span></span><br><span class="line"><span class="comment">* ofNanoOfDay(long nanoOfDay)</span></span><br><span class="line"><span class="comment">* 作用：给定距离00:00的秒数或纳秒数创建localTime对象。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test03</span><span class="params">()</span>&#123;</span><br><span class="line">    System.out.println(LocalTime.of(<span class="number">12</span>, <span class="number">30</span>)); <span class="comment">// 输出 12:30</span></span><br><span class="line">    System.out.println(LocalTime.of(<span class="number">12</span>, <span class="number">30</span>, <span class="number">45</span>)); <span class="comment">// 输出 12:30:45</span></span><br><span class="line">    System.out.println(LocalTime.of(<span class="number">12</span>, <span class="number">30</span>, <span class="number">45</span>, <span class="number">100</span>)); <span class="comment">// 输出 12:30:45.000000100</span></span><br><span class="line"></span><br><span class="line">    System.out.println(LocalTime.ofSecondOfDay(<span class="number">0</span>)); <span class="comment">// 输出 00:00</span></span><br><span class="line">    System.out.println(LocalTime.ofSecondOfDay(<span class="number">1</span> * <span class="number">60</span> * <span class="number">60</span> + <span class="number">1</span>)); <span class="comment">// 输出 01:00:01</span></span><br><span class="line">    System.out.println(LocalTime.ofNanoOfDay(<span class="number">3</span> * <span class="number">60</span> * <span class="number">60</span> * <span class="number">1000_000_000L</span> + <span class="number">1</span>)); <span class="comment">// 输出 03:00:00.000000001</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="parse-CharSequence-text"><a href="#parse-CharSequence-text" class="headerlink" title="parse(CharSequence text)"></a>parse(CharSequence text)</h4><h4 id="parse-CharSequence-text-DateTimeFormatter-formatter"><a href="#parse-CharSequence-text-DateTimeFormatter-formatter" class="headerlink" title="parse(CharSequence text, DateTimeFormatter formatter)"></a>parse(CharSequence text, DateTimeFormatter formatter)</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* parse(CharSequence text)</span></span><br><span class="line"><span class="comment">* parse(CharSequence text, DateTimeFormatter formatter)</span></span><br><span class="line"><span class="comment">* 作用：将字符串解析成localTme对象，默认格式HH:mm或HH:mm:ss，formatter参数用来指定字符串的格式。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test04</span><span class="params">()</span>&#123;</span><br><span class="line">    System.out.println(LocalTime.parse(<span class="string">&quot;12:30&quot;</span>)); <span class="comment">// 输出 12:30</span></span><br><span class="line">    System.out.println(LocalTime.parse(<span class="string">&quot;12:30:45&quot;</span>)); <span class="comment">// 输出 12:30:45</span></span><br><span class="line"></span><br><span class="line">    <span class="type">DateTimeFormatter</span> <span class="variable">df01</span> <span class="operator">=</span> DateTimeFormatter.ofPattern(<span class="string">&quot;HH/mm/ss&quot;</span>);</span><br><span class="line">    System.out.println(LocalTime.parse(<span class="string">&quot;12/30/45&quot;</span>, df01)); <span class="comment">// 输出 12:30:45</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<br>

<h3 id="成员方法"><a href="#成员方法" class="headerlink" title="成员方法"></a>成员方法</h3><h4 id="atDate-LocalDate-date"><a href="#atDate-LocalDate-date" class="headerlink" title="atDate(LocalDate date)"></a>atDate(LocalDate date)</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* atDate(LocalDate date)</span></span><br><span class="line"><span class="comment">* 作用：补充年月日数据后生成localDateTime对象。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test01</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="type">LocalTime</span> <span class="variable">time01</span> <span class="operator">=</span> LocalTime.parse(<span class="string">&quot;12:30:45&quot;</span>);</span><br><span class="line">    <span class="type">LocalDate</span> <span class="variable">date01</span> <span class="operator">=</span> LocalDate.parse(<span class="string">&quot;2023-01-01&quot;</span>);</span><br><span class="line">    System.out.println(time01.atDate(date01)); <span class="comment">// 输出 2023-01-01T12:30:45</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="atOffset-ZoneOffset-offset"><a href="#atOffset-ZoneOffset-offset" class="headerlink" title="atOffset(ZoneOffset offset)"></a>atOffset(ZoneOffset offset)</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* atOffset(ZoneOffset offset)</span></span><br><span class="line"><span class="comment">* 作用：时间不变，仅和offset偏移量组合生成offsetTime对象。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test02</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="type">LocalTime</span> <span class="variable">time01</span> <span class="operator">=</span> LocalTime.parse(<span class="string">&quot;12:00&quot;</span>);</span><br><span class="line">    <span class="type">ZoneOffset</span> <span class="variable">zoneOffset01</span> <span class="operator">=</span> ZoneOffset.of(<span class="string">&quot;+08:00&quot;</span>);</span><br><span class="line">    System.out.println(time01.atOffset(zoneOffset01)); <span class="comment">// 输出 12:00+08:00</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="compareTo-LocalTime-other"><a href="#compareTo-LocalTime-other" class="headerlink" title="compareTo(LocalTime other)"></a>compareTo(LocalTime other)</h4><h4 id="isAfter-LocalTime-other"><a href="#isAfter-LocalTime-other" class="headerlink" title="isAfter(LocalTime other)"></a>isAfter(LocalTime other)</h4><h4 id="isBefore-LocalTime-other"><a href="#isBefore-LocalTime-other" class="headerlink" title="isBefore(LocalTime other)"></a>isBefore(LocalTime other)</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* compareTo(LocalTime other)</span></span><br><span class="line"><span class="comment">* isAfter(LocalTime other)</span></span><br><span class="line"><span class="comment">* isBefore(LocalTime other)</span></span><br><span class="line"><span class="comment">* 作用：比较两个localTime对象的大小，other为null时报NPE异常。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test03</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="type">LocalTime</span> <span class="variable">time01</span> <span class="operator">=</span> LocalTime.of(<span class="number">12</span>, <span class="number">30</span>, <span class="number">45</span>);</span><br><span class="line">    <span class="type">LocalTime</span> <span class="variable">time02</span> <span class="operator">=</span> LocalTime.of(<span class="number">12</span>, <span class="number">30</span>, <span class="number">45</span>);</span><br><span class="line">    <span class="type">LocalTime</span> <span class="variable">time03</span> <span class="operator">=</span> LocalTime.of(<span class="number">12</span>, <span class="number">30</span>, <span class="number">50</span>);</span><br><span class="line">    <span class="type">LocalTime</span> <span class="variable">time04</span> <span class="operator">=</span> <span class="literal">null</span>;</span><br><span class="line"></span><br><span class="line">    System.out.println(time01.compareTo(time02)); <span class="comment">// 输出 0</span></span><br><span class="line">    System.out.println(time01.compareTo(time03)); <span class="comment">// 输出 -1</span></span><br><span class="line">    System.out.println(time01.compareTo(time04)); <span class="comment">// NPE异常</span></span><br><span class="line"></span><br><span class="line">    System.out.println(time01.isBefore(time03)); <span class="comment">// 输出 true</span></span><br><span class="line">    System.out.println(time01.isBefore(time04)); <span class="comment">// NPE异常</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="format-DateTimeFormatter-formatter"><a href="#format-DateTimeFormatter-formatter" class="headerlink" title="format(DateTimeFormatter formatter)"></a>format(DateTimeFormatter formatter)</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* format(DateTimeFormatter formatter)</span></span><br><span class="line"><span class="comment">* 作用：按指定格式化输出字符串。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test04</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="type">LocalTime</span> <span class="variable">time01</span> <span class="operator">=</span> LocalTime.of(<span class="number">12</span>, <span class="number">30</span>, <span class="number">45</span>);</span><br><span class="line">    <span class="type">DateTimeFormatter</span> <span class="variable">df01</span> <span class="operator">=</span> DateTimeFormatter.ofPattern(<span class="string">&quot;HH时mm分ss秒&quot;</span>);</span><br><span class="line">    System.out.println(time01.format(df01)); <span class="comment">// 输出 12时30分45秒</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="isSupported-TemporalField-field"><a href="#isSupported-TemporalField-field" class="headerlink" title="isSupported(TemporalField field)"></a>isSupported(TemporalField field)</h4><h4 id="isSupported-TemporalUnit-unit"><a href="#isSupported-TemporalUnit-unit" class="headerlink" title="isSupported(TemporalUnit unit)"></a>isSupported(TemporalUnit unit)</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* isSupported(TemporalField field)</span></span><br><span class="line"><span class="comment">* 作用：判断对象是否支持指定的时间分量。</span></span><br><span class="line"><span class="comment">*</span></span><br><span class="line"><span class="comment">* isSupported(TemporalUnit unit)</span></span><br><span class="line"><span class="comment">* 作用：判断对象是否支持指定的时间单位。时间单位有年、月、日、时、分、秒、毫秒、纳秒、微妙等等。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test05</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="type">LocalTime</span> <span class="variable">time01</span> <span class="operator">=</span> LocalTime.of(<span class="number">12</span>, <span class="number">30</span>, <span class="number">45</span>);</span><br><span class="line">    System.out.println(time01.isSupported(ChronoField.HOUR_OF_DAY)); <span class="comment">// 输出 true</span></span><br><span class="line">    System.out.println(time01.isSupported(ChronoField.YEAR)); <span class="comment">// 输出 false</span></span><br><span class="line"></span><br><span class="line">    System.out.println(time01.isSupported(ChronoUnit.HOURS)); <span class="comment">// 输出 true</span></span><br><span class="line">    System.out.println(time01.isSupported(ChronoUnit.YEARS)); <span class="comment">// 输出 false</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="get-TemporalField-field"><a href="#get-TemporalField-field" class="headerlink" title="get(TemporalField field)"></a>get(TemporalField field)</h4><h4 id="getLong-TemporalField-field"><a href="#getLong-TemporalField-field" class="headerlink" title="getLong(TemporalField field)"></a>getLong(TemporalField field)</h4><h4 id="getHour"><a href="#getHour" class="headerlink" title="getHour()"></a>getHour()</h4><h4 id="getMinute"><a href="#getMinute" class="headerlink" title="getMinute()"></a>getMinute()</h4><h4 id="getNano"><a href="#getNano" class="headerlink" title="getNano()"></a>getNano()</h4><h4 id="getSecond"><a href="#getSecond" class="headerlink" title="getSecond()"></a>getSecond()</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* get(TemporalField field)</span></span><br><span class="line"><span class="comment">* getLong(TemporalField field)</span></span><br><span class="line"><span class="comment">* 作用：获取指定的时间分量，如年、月、日、时、分、秒、星期、一年中的第几天等等，详细可查看ChronoField类。</span></span><br><span class="line"><span class="comment">*      但不是所有时间分量都能获取。如localTime对象无法获取跟年、月、日相关的时间分量。</span></span><br><span class="line"><span class="comment">*</span></span><br><span class="line"><span class="comment">* getHour()</span></span><br><span class="line"><span class="comment">* getMinute()</span></span><br><span class="line"><span class="comment">* getNano()</span></span><br><span class="line"><span class="comment">* getSecond()</span></span><br><span class="line"><span class="comment">* 作用：作用类似get(TemporalField field)。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test06</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="type">LocalTime</span> <span class="variable">time01</span> <span class="operator">=</span> LocalTime.of(<span class="number">12</span>, <span class="number">30</span>, <span class="number">45</span>);</span><br><span class="line">    System.out.println(time01.get(ChronoField.HOUR_OF_DAY)); <span class="comment">// 输出 12</span></span><br><span class="line">    System.out.println(time01.get(ChronoField.MINUTE_OF_HOUR)); <span class="comment">// 输出 30</span></span><br><span class="line">    System.out.println(time01.get(ChronoField.MINUTE_OF_DAY)); <span class="comment">// 输出 750</span></span><br><span class="line"></span><br><span class="line">    System.out.println(time01.getHour()); <span class="comment">// 输出 12</span></span><br><span class="line">    System.out.println(time01.getMinute()); <span class="comment">// 输出 30</span></span><br><span class="line">    System.out.println(time01.getSecond()); <span class="comment">// 输出 45</span></span><br><span class="line">    System.out.println(time01.getNano()); <span class="comment">// 输出 0</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="minus-long-amountToSubtract-TemporalUnit-unit"><a href="#minus-long-amountToSubtract-TemporalUnit-unit" class="headerlink" title="minus(long amountToSubtract, TemporalUnit unit)"></a>minus(long amountToSubtract, TemporalUnit unit)</h4><h4 id="minus-TemporalAmount-amountToSubtract"><a href="#minus-TemporalAmount-amountToSubtract" class="headerlink" title="minus(TemporalAmount amountToSubtract)"></a>minus(TemporalAmount amountToSubtract)</h4><h4 id="minusHours-long-hoursToSubtract"><a href="#minusHours-long-hoursToSubtract" class="headerlink" title="minusHours(long hoursToSubtract)"></a>minusHours(long hoursToSubtract)</h4><h4 id="minusMinutes-long-minutesToSubtract"><a href="#minusMinutes-long-minutesToSubtract" class="headerlink" title="minusMinutes(long minutesToSubtract)"></a>minusMinutes(long minutesToSubtract)</h4><h4 id="minusNanos-long-nanosToSubtract"><a href="#minusNanos-long-nanosToSubtract" class="headerlink" title="minusNanos(long nanosToSubtract)"></a>minusNanos(long nanosToSubtract)</h4><h4 id="minusSeconds-long-secondsToSubtract"><a href="#minusSeconds-long-secondsToSubtract" class="headerlink" title="minusSeconds(long secondsToSubtract)"></a>minusSeconds(long secondsToSubtract)</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* minus(long amountToSubtract, TemporalUnit unit)</span></span><br><span class="line"><span class="comment">* minus(TemporalAmount amountToSubtract)</span></span><br><span class="line"><span class="comment">* minusHours(long hoursToSubtract)</span></span><br><span class="line"><span class="comment">* minusMinutes(long minutesToSubtract)</span></span><br><span class="line"><span class="comment">* minusNanos(long nanosToSubtract)</span></span><br><span class="line"><span class="comment">* minusSeconds(long secondsToSubtract)</span></span><br><span class="line"><span class="comment">* 作用：在指定的时间单位上减去固定值。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test07</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="type">LocalTime</span> <span class="variable">time01</span> <span class="operator">=</span> LocalTime.of(<span class="number">12</span>, <span class="number">30</span>, <span class="number">45</span>);</span><br><span class="line">    System.out.println(time01.minus(<span class="number">5</span>, ChronoUnit.SECONDS)); <span class="comment">// 输出 12:30:40</span></span><br><span class="line"></span><br><span class="line">    <span class="type">Duration</span> <span class="variable">duration01</span> <span class="operator">=</span> Duration.ofSeconds(<span class="number">5</span>);</span><br><span class="line">    System.out.println(time01.minus(duration01)); <span class="comment">// 输出 12:30:40</span></span><br><span class="line"></span><br><span class="line">    System.out.println(time01.minusHours(<span class="number">2</span>)); <span class="comment">// 输出 10:30:45</span></span><br><span class="line">    System.out.println(time01.minusMinutes(<span class="number">15</span>)); <span class="comment">// 输出 12:15:45</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="plus-long-amountToSubtract-TemporalUnit-unit"><a href="#plus-long-amountToSubtract-TemporalUnit-unit" class="headerlink" title="plus(long amountToSubtract, TemporalUnit unit)"></a>plus(long amountToSubtract, TemporalUnit unit)</h4><h4 id="plus-TemporalAmount-amountToSubtract"><a href="#plus-TemporalAmount-amountToSubtract" class="headerlink" title="plus(TemporalAmount amountToSubtract)"></a>plus(TemporalAmount amountToSubtract)</h4><h4 id="plusHours-long-hoursToAdd"><a href="#plusHours-long-hoursToAdd" class="headerlink" title="plusHours(long hoursToAdd)"></a>plusHours(long hoursToAdd)</h4><h4 id="plusMinutes-long-minutesToAdd"><a href="#plusMinutes-long-minutesToAdd" class="headerlink" title="plusMinutes(long minutesToAdd)"></a>plusMinutes(long minutesToAdd)</h4><h4 id="plusNanos-long-nanosToAdd"><a href="#plusNanos-long-nanosToAdd" class="headerlink" title="plusNanos(long nanosToAdd)"></a>plusNanos(long nanosToAdd)</h4><h4 id="plusSeconds-long-secondstoAdd"><a href="#plusSeconds-long-secondstoAdd" class="headerlink" title="plusSeconds(long secondstoAdd)"></a>plusSeconds(long secondstoAdd)</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* plus(long amountToSubtract, TemporalUnit unit)</span></span><br><span class="line"><span class="comment">* plus(TemporalAmount amountToSubtract)</span></span><br><span class="line"><span class="comment">* plusHours(long hoursToAdd)</span></span><br><span class="line"><span class="comment">* plusMinutes(long minutesToAdd)</span></span><br><span class="line"><span class="comment">* plusNanos(long nanosToAdd)</span></span><br><span class="line"><span class="comment">* plusSeconds(long secondstoAdd)</span></span><br><span class="line"><span class="comment">* 作用：在指定的时间单位上加上固定值。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test08</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="type">LocalTime</span> <span class="variable">time01</span> <span class="operator">=</span> LocalTime.of(<span class="number">12</span>, <span class="number">30</span>, <span class="number">45</span>);</span><br><span class="line">    System.out.println(time01.plus(<span class="number">5</span>, ChronoUnit.SECONDS)); <span class="comment">// 输出 12:30:50</span></span><br><span class="line"></span><br><span class="line">    <span class="type">Duration</span> <span class="variable">duration01</span> <span class="operator">=</span> Duration.ofSeconds(<span class="number">5</span>);</span><br><span class="line">    System.out.println(time01.plus(duration01)); <span class="comment">// 输出 12:30:50</span></span><br><span class="line"></span><br><span class="line">    System.out.println(time01.plusHours(<span class="number">2</span>)); <span class="comment">// 输出 14:30:45</span></span><br><span class="line">    System.out.println(time01.plusMinutes(<span class="number">15</span>)); <span class="comment">// 输出 12:45:45</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="toSecondOfDay"><a href="#toSecondOfDay" class="headerlink" title="toSecondOfDay()"></a>toSecondOfDay()</h4><h4 id="toNanoOfDay"><a href="#toNanoOfDay" class="headerlink" title="toNanoOfDay()"></a>toNanoOfDay()</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* toSecondOfDay()</span></span><br><span class="line"><span class="comment">* toNanoOfDay()</span></span><br><span class="line"><span class="comment">* 作用：计算localTime距离00:00的秒数或纳秒数。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test09</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="type">LocalTime</span> <span class="variable">time01</span> <span class="operator">=</span> LocalTime.of(<span class="number">1</span>, <span class="number">0</span>, <span class="number">0</span>);</span><br><span class="line">    System.out.println(time01.toSecondOfDay()); <span class="comment">// 输出 3600</span></span><br><span class="line">    System.out.println(time01.toNanoOfDay()); <span class="comment">// 输出 3600000000000</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="query-TemporalQuery-lt-R-gt-query"><a href="#query-TemporalQuery-lt-R-gt-query" class="headerlink" title="query(TemporalQuery&lt;R&gt; query)"></a>query(TemporalQuery&lt;R&gt; query)</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* query(TemporalQuery&lt;R&gt; query)</span></span><br><span class="line"><span class="comment">* 作用：TemporalQuery是一个函数接口，包含一个queryFrom(TemporalAccessor temporal)方法。</span></span><br><span class="line"><span class="comment">*      其中的temporal对象就是调用query()方法的对象。通过实现TemporalQuery接口来自定义查询日期的逻辑，</span></span><br><span class="line"><span class="comment">*      比如我有个需求，需要经常计算localTime距离12:00还差多少秒，官方没有提供这样的查询逻辑而我们又</span></span><br><span class="line"><span class="comment">*      经常需要用到，此时我们可以实现TemporalQuery接口来自定义查询逻辑。</span></span><br><span class="line"><span class="comment">*      TemporalQueries类已经为我们定义了常用的查询逻辑。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test10</span><span class="params">()</span>&#123;</span><br><span class="line">    TemporalQuery&lt;Integer&gt; toSecondOfNoon = time -&gt;&#123;</span><br><span class="line">        <span class="keyword">if</span>(time.isSupported(ChronoField.HOUR_OF_DAY) &amp;&amp;</span><br><span class="line">           time.isSupported(ChronoField.MINUTE_OF_HOUR) &amp;&amp;</span><br><span class="line">           time.isSupported(ChronoField.SECOND_OF_MINUTE))&#123;</span><br><span class="line"></span><br><span class="line">            <span class="type">int</span> <span class="variable">noon</span> <span class="operator">=</span> LocalTime.NOON.toSecondOfDay();</span><br><span class="line">            <span class="type">int</span> <span class="variable">secondsPerMinute</span> <span class="operator">=</span> <span class="number">60</span>;</span><br><span class="line">            <span class="type">int</span> <span class="variable">secodsPerHour</span> <span class="operator">=</span> <span class="number">3600</span>;</span><br><span class="line"></span><br><span class="line">            <span class="type">int</span> <span class="variable">hour</span> <span class="operator">=</span> time.get(ChronoField.HOUR_OF_DAY);</span><br><span class="line">            <span class="type">int</span> <span class="variable">minute</span> <span class="operator">=</span> time.get(ChronoField.MINUTE_OF_HOUR);</span><br><span class="line">            <span class="type">int</span> <span class="variable">second</span> <span class="operator">=</span> time.get(ChronoField.SECOND_OF_MINUTE);</span><br><span class="line"></span><br><span class="line">            <span class="type">int</span> <span class="variable">total</span> <span class="operator">=</span> hour * secodsPerHour;</span><br><span class="line">            total += minute * secondsPerMinute;</span><br><span class="line">            total += second;</span><br><span class="line"></span><br><span class="line">            <span class="keyword">return</span> noon - total;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line">    &#125;;</span><br><span class="line"></span><br><span class="line">    <span class="type">LocalTime</span> <span class="variable">time01</span> <span class="operator">=</span> LocalTime.of(<span class="number">11</span>, <span class="number">59</span>, <span class="number">59</span>);</span><br><span class="line">    <span class="type">LocalTime</span> <span class="variable">time02</span> <span class="operator">=</span> LocalTime.of(<span class="number">11</span>, <span class="number">0</span>, <span class="number">0</span>);</span><br><span class="line">    System.out.println(time01.query(toSecondOfNoon)); <span class="comment">// 输出 1</span></span><br><span class="line">    System.out.println(time02.query(toSecondOfNoon)); <span class="comment">// 输出 3600</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="range-TemporalField-field"><a href="#range-TemporalField-field" class="headerlink" title="range(TemporalField field)"></a>range(TemporalField field)</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* range(TemporalField field)</span></span><br><span class="line"><span class="comment">* 作用：输出与localTime对象相关的时间分量值的范围。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test11</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="type">LocalTime</span> <span class="variable">time01</span> <span class="operator">=</span> LocalTime.of(<span class="number">12</span>, <span class="number">30</span>, <span class="number">45</span>);</span><br><span class="line">    System.out.println(time01.range(ChronoField.HOUR_OF_DAY)); <span class="comment">// 输出 0 - 23</span></span><br><span class="line">    System.out.println(time01.range(ChronoField.SECOND_OF_MINUTE)); <span class="comment">// 输出 0 - 59</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="truncatedTo-TemporalUnit-unit"><a href="#truncatedTo-TemporalUnit-unit" class="headerlink" title="truncatedTo(TemporalUnit unit)"></a>truncatedTo(TemporalUnit unit)</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* truncatedTo(TemporalUnit unit)</span></span><br><span class="line"><span class="comment">* 作用：根据时间单位截取localTime对象。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test12</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="type">LocalTime</span> <span class="variable">time01</span> <span class="operator">=</span> LocalTime.of(<span class="number">12</span>, <span class="number">30</span>, <span class="number">45</span>, <span class="number">100</span>);</span><br><span class="line">    System.out.println(time01.truncatedTo(ChronoUnit.SECONDS)); <span class="comment">// 输出 12:30:45</span></span><br><span class="line">    System.out.println(time01.truncatedTo(ChronoUnit.MINUTES)); <span class="comment">// 输出 12:30</span></span><br><span class="line">    System.out.println(time01.truncatedTo(ChronoUnit.HOURS)); <span class="comment">// 输出 12:00</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="until-Temporal-endExclusive-TemporalUnit-unit"><a href="#until-Temporal-endExclusive-TemporalUnit-unit" class="headerlink" title="until(Temporal endExclusive, TemporalUnit unit)"></a>until(Temporal endExclusive, TemporalUnit unit)</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* until(Temporal endExclusive, TemporalUnit unit)</span></span><br><span class="line"><span class="comment">* 作用：根据时间单位计算两个localTime对象的差值。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test13</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="type">LocalTime</span> <span class="variable">time01</span> <span class="operator">=</span> LocalTime.of(<span class="number">11</span>, <span class="number">15</span>, <span class="number">25</span>);</span><br><span class="line">    <span class="type">LocalTime</span> <span class="variable">time02</span> <span class="operator">=</span> LocalTime.of(<span class="number">12</span>, <span class="number">30</span>, <span class="number">45</span>);</span><br><span class="line">    <span class="type">LocalDateTime</span> <span class="variable">dateTime01</span> <span class="operator">=</span> LocalDateTime.of(<span class="number">2023</span>, <span class="number">1</span>,<span class="number">1</span>, <span class="number">12</span>, <span class="number">30</span>, <span class="number">45</span>);</span><br><span class="line"></span><br><span class="line">    System.out.println(time01.until(time02, ChronoUnit.HOURS)); <span class="comment">// 输出 1</span></span><br><span class="line">    System.out.println(time01.until(time02, ChronoUnit.MINUTES)); <span class="comment">// 输出 75</span></span><br><span class="line">    System.out.println(time01.until(dateTime01, ChronoUnit.SECONDS)); <span class="comment">// 输出 4520</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="with-TemporalField-field-long-newValue"><a href="#with-TemporalField-field-long-newValue" class="headerlink" title="with(TemporalField field, long newValue)"></a>with(TemporalField field, long newValue)</h4><h4 id="withHour-int-hour"><a href="#withHour-int-hour" class="headerlink" title="withHour(int hour)"></a>withHour(int hour)</h4><h4 id="withMinute-int-minute"><a href="#withMinute-int-minute" class="headerlink" title="withMinute(int minute)"></a>withMinute(int minute)</h4><h4 id="withNano-int-nanoOfSecond"><a href="#withNano-int-nanoOfSecond" class="headerlink" title="withNano(int nanoOfSecond)"></a>withNano(int nanoOfSecond)</h4><h4 id="withSecond-int-second"><a href="#withSecond-int-second" class="headerlink" title="withSecond(int second)"></a>withSecond(int second)</h4><h4 id="with-TemporalAdjuster-adjuster"><a href="#with-TemporalAdjuster-adjuster" class="headerlink" title="with(TemporalAdjuster adjuster)"></a>with(TemporalAdjuster adjuster)</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* with(TemporalField field, long newValue)</span></span><br><span class="line"><span class="comment">* withHour(int hour)</span></span><br><span class="line"><span class="comment">* withMinute(int minute)</span></span><br><span class="line"><span class="comment">* withNano(int nanoOfSecond)</span></span><br><span class="line"><span class="comment">* withSecond(int second)</span></span><br><span class="line"><span class="comment">* 作用：调整跟localDate对象相关的时间分量的值。</span></span><br><span class="line"><span class="comment">*</span></span><br><span class="line"><span class="comment">* with(TemporalAdjuster adjuster)</span></span><br><span class="line"><span class="comment">* 作用：该方法跟query(TemporalQuery&lt;R&gt; query)类似，query()方法是根据给定的查询逻辑查询localTime对象。</span></span><br><span class="line"><span class="comment">* 而with()方法是根据给定的修改逻辑修改localTime对象，java8的所有时间类都是不可变的，所以修改后返回的都是新的localTime对象。</span></span><br><span class="line"><span class="comment">* TemporalAdjuster也是一个函数接口，包含Temporal adjustInto(Temporal temporal)方法。</span></span><br><span class="line"><span class="comment">* 其中temporal对象就是调用with()方法的对象，通过实现TemporalAdjuster接口实现对localTime对象的修改逻辑。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">test14</span><span class="params">()</span>&#123;</span><br><span class="line">    <span class="type">LocalTime</span> <span class="variable">time01</span> <span class="operator">=</span> LocalTime.of(<span class="number">12</span>, <span class="number">30</span>, <span class="number">45</span>);</span><br><span class="line">    <span class="type">LocalTime</span> <span class="variable">time02</span> <span class="operator">=</span> LocalTime.of(<span class="number">12</span>, <span class="number">30</span>, <span class="number">15</span>);</span><br><span class="line">    System.out.println(time01.with(ChronoField.HOUR_OF_DAY, <span class="number">18</span>)); <span class="comment">// 输出 18:30:45</span></span><br><span class="line">    System.out.println(time01.with(ChronoField.MINUTE_OF_HOUR, <span class="number">45</span>)); <span class="comment">// 输出 12:45:45</span></span><br><span class="line"></span><br><span class="line">    System.out.println(time01.withHour(<span class="number">18</span>)); <span class="comment">// 输出 18:30:45</span></span><br><span class="line">    System.out.println(time01.withMinute(<span class="number">45</span>)); <span class="comment">// 输出 12:45:45</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">/** 对时间进行四舍五入，大于30秒分+1，小于30秒抹去 **/</span></span><br><span class="line">    <span class="type">TemporalAdjuster</span> <span class="variable">adjuster</span> <span class="operator">=</span> time -&gt; &#123;</span><br><span class="line">        <span class="keyword">if</span> (time.isSupported(ChronoField.SECOND_OF_MINUTE) &amp;&amp; time.isSupported(ChronoField.MINUTE_OF_HOUR)) &#123;</span><br><span class="line">            <span class="type">int</span> <span class="variable">seconds</span> <span class="operator">=</span> time.get(ChronoField.SECOND_OF_MINUTE);</span><br><span class="line">            time = time.with(ChronoField.SECOND_OF_MINUTE, <span class="number">0</span>);</span><br><span class="line">            Temporal result;</span><br><span class="line">            <span class="keyword">if</span> (seconds &gt;= <span class="number">30</span>) &#123;</span><br><span class="line">                <span class="type">int</span> <span class="variable">minute</span> <span class="operator">=</span> time.get(ChronoField.MINUTE_OF_HOUR);</span><br><span class="line">                time = time.with(ChronoField.MINUTE_OF_HOUR, minute + <span class="number">1</span>);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">return</span> time;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line">    &#125;;</span><br><span class="line">    System.out.println(time01.with(adjuster)); <span class="comment">// 输出 12:31</span></span><br><span class="line">    System.out.println(time02.with(adjuster)); <span class="comment">// 输出 12:30</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="未演示方法"><a href="#未演示方法" class="headerlink" title="未演示方法"></a>未演示方法</h4><ol>
<li><p>adjustInto(Temporal temporal)</p>
</li>
<li><p>equals(Object obj)</p>
</li>
<li><p>hashCode()</p>
</li>
<li><p>toString()</p>
</li>
</ol>
<h3 id="参考文献"><a href="#参考文献" class="headerlink" title="参考文献"></a>参考文献</h3><p><a target="_blank" rel="noopener" href="https://www.matools.com/api/java8">https://www.matools.com/api/java8</a></p>
</div>
        </div>
        
        <footer class="kratos-entry-footer clearfix">
            
            <div class="footer-tag clearfix">
                <div class="pull-left">
                <i class="fa fa-tags"></i>
                    <a class="tag-none-link" href="/blog/tags/Java/" rel="tag">Java</a>
                </div>
                <div class="pull-date">
                    <time datetime="2025-11-20T11:35:22.178Z" itemprop="dateModified">最后编辑：2025-11-20</time>
                </div>
            </div>
        </footer>
    </div>
    
        <nav class="navigation post-navigation clearfix" role="navigation">
            
            <div class="nav-previous clearfix">
                <a title=" 【Java 8 新特性】时间类：LocalDate" href="/blog/2023/05/04/Java8/time/LocalDate/">&lt; 上一篇</a>
            </div>
            
            
            <div class="nav-next clearfix">
                <a title=" 【Java 8 新特性】时间类：LocalDateTime" href="/blog/2023/05/08/Java8/time/LocalDateTime/">下一篇 &gt;</a>
            </div>
            
        </nav>
    
    
</article>

        

            </section>

        

                
            

<section id="kratos-widget-area" class="col-md-4 hidden-xs hidden-sm">
    <!-- 文章和页面根据splitter来分割，没有的话就从头开始设置为sticky -->
    
    
                <aside id="krw-about" class="widget widget-kratos-about clearfix">
    <div class="photo-background"></div>
    <div class="photo-wrapper clearfix">
        <div class="photo-wrapper-tip text-center">
            <img class="about-photo" src="/blog/images/avatar.webp" loading="lazy" decoding="auto" />
        </div>
    </div>
    <div class="textwidget">
        <p class="text-center">极简主义</p>
    </div>
    <div class="site-meta">
        <a class="meta-item" href="/blog/archives/">
            <span class="title">
                文章
            </span>
            <span class="count">
                72
            </span>
        </a>
        <a class="meta-item" href="/blog/categories/">
            <span class="title">
                分类
            </span>
            <span class="count">
                9
            </span>
        </a>
        <a class="meta-item" href="/blog/tags/">
            <span class="title">
                标签
            </span>
            <span class="count">
                27
            </span>
        </a>
    </div>
</aside>
            
                    <div class="sticky-area">
                
                    <aside id="krw-toc" class="widget widget-kratos-toc clearfix toc-div-class" >
    <div class="photo-background"></div>
    <h4 class="widget-title no-after">
        <i class="fa fa-compass"></i>
        文章目录
        <span class="toc-progress-bar" role="progressbar" aria-label="阅读进度："></span>
    </h4>
    <div class="textwidget">
        <ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%AC%E5%85%B1%E5%B8%B8%E9%87%8F"><span class="toc-text">公共常量</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9D%99%E6%80%81%E6%96%B9%E6%B3%95"><span class="toc-text">静态方法</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#from-TemporalAccessor-temporal"><span class="toc-text">from(TemporalAccessor temporal)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#now"><span class="toc-text">now()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#now-ZoneId-zone"><span class="toc-text">now(ZoneId zone)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#now-Clock-clock"><span class="toc-text">now(Clock clock)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#of-int-hour-int-minute"><span class="toc-text">of(int hour, int minute)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#of-int-hour-int-minute-int-second"><span class="toc-text">of(int hour, int minute, int second)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#of-int-hour-int-minute-int-second-int-nanoOfSecond"><span class="toc-text">of(int hour, int minute, int second, int nanoOfSecond)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#ofSecondOfDay-long-secondOfDay"><span class="toc-text">ofSecondOfDay(long secondOfDay)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#ofNanoOfDay-long-nanoOfDay"><span class="toc-text">ofNanoOfDay(long nanoOfDay)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#parse-CharSequence-text"><span class="toc-text">parse(CharSequence text)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#parse-CharSequence-text-DateTimeFormatter-formatter"><span class="toc-text">parse(CharSequence text, DateTimeFormatter formatter)</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%88%90%E5%91%98%E6%96%B9%E6%B3%95"><span class="toc-text">成员方法</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#atDate-LocalDate-date"><span class="toc-text">atDate(LocalDate date)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#atOffset-ZoneOffset-offset"><span class="toc-text">atOffset(ZoneOffset offset)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#compareTo-LocalTime-other"><span class="toc-text">compareTo(LocalTime other)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#isAfter-LocalTime-other"><span class="toc-text">isAfter(LocalTime other)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#isBefore-LocalTime-other"><span class="toc-text">isBefore(LocalTime other)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#format-DateTimeFormatter-formatter"><span class="toc-text">format(DateTimeFormatter formatter)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#isSupported-TemporalField-field"><span class="toc-text">isSupported(TemporalField field)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#isSupported-TemporalUnit-unit"><span class="toc-text">isSupported(TemporalUnit unit)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#get-TemporalField-field"><span class="toc-text">get(TemporalField field)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#getLong-TemporalField-field"><span class="toc-text">getLong(TemporalField field)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#getHour"><span class="toc-text">getHour()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#getMinute"><span class="toc-text">getMinute()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#getNano"><span class="toc-text">getNano()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#getSecond"><span class="toc-text">getSecond()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#minus-long-amountToSubtract-TemporalUnit-unit"><span class="toc-text">minus(long amountToSubtract, TemporalUnit unit)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#minus-TemporalAmount-amountToSubtract"><span class="toc-text">minus(TemporalAmount amountToSubtract)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#minusHours-long-hoursToSubtract"><span class="toc-text">minusHours(long hoursToSubtract)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#minusMinutes-long-minutesToSubtract"><span class="toc-text">minusMinutes(long minutesToSubtract)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#minusNanos-long-nanosToSubtract"><span class="toc-text">minusNanos(long nanosToSubtract)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#minusSeconds-long-secondsToSubtract"><span class="toc-text">minusSeconds(long secondsToSubtract)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#plus-long-amountToSubtract-TemporalUnit-unit"><span class="toc-text">plus(long amountToSubtract, TemporalUnit unit)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#plus-TemporalAmount-amountToSubtract"><span class="toc-text">plus(TemporalAmount amountToSubtract)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#plusHours-long-hoursToAdd"><span class="toc-text">plusHours(long hoursToAdd)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#plusMinutes-long-minutesToAdd"><span class="toc-text">plusMinutes(long minutesToAdd)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#plusNanos-long-nanosToAdd"><span class="toc-text">plusNanos(long nanosToAdd)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#plusSeconds-long-secondstoAdd"><span class="toc-text">plusSeconds(long secondstoAdd)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#toSecondOfDay"><span class="toc-text">toSecondOfDay()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#toNanoOfDay"><span class="toc-text">toNanoOfDay()</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#query-TemporalQuery-lt-R-gt-query"><span class="toc-text">query(TemporalQuery&lt;R&gt; query)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#range-TemporalField-field"><span class="toc-text">range(TemporalField field)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#truncatedTo-TemporalUnit-unit"><span class="toc-text">truncatedTo(TemporalUnit unit)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#until-Temporal-endExclusive-TemporalUnit-unit"><span class="toc-text">until(Temporal endExclusive, TemporalUnit unit)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#with-TemporalField-field-long-newValue"><span class="toc-text">with(TemporalField field, long newValue)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#withHour-int-hour"><span class="toc-text">withHour(int hour)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#withMinute-int-minute"><span class="toc-text">withMinute(int minute)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#withNano-int-nanoOfSecond"><span class="toc-text">withNano(int nanoOfSecond)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#withSecond-int-second"><span class="toc-text">withSecond(int second)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#with-TemporalAdjuster-adjuster"><span class="toc-text">with(TemporalAdjuster adjuster)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%9C%AA%E6%BC%94%E7%A4%BA%E6%96%B9%E6%B3%95"><span class="toc-text">未演示方法</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8F%82%E8%80%83%E6%96%87%E7%8C%AE"><span class="toc-text">参考文献</span></a></li></ol>
    </div>
</aside>
                
                
  <aside id="krw-categories" class="widget widget-kratos-categories clearfix">
    <h4 class="widget-title"><i class="fa fa-folder"></i>分类目录</h4>
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/blog/categories/Docker/">Docker</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/Idea/">Idea</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/Java/">Java</a><span class="category-list-count">11</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/Linux/">Linux</a><span class="category-list-count">7</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/MySQL/">MySQL</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/Python/">Python</a><span class="category-list-count">11</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/Selenium/">Selenium</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/SpringBoot/">SpringBoot</a><span class="category-list-count">4</span></li><li class="category-list-item"><a class="category-list-link" href="/blog/categories/SpringCloud/">SpringCloud</a><span class="category-list-count">13</span></li></ul>
  </aside>


            
                
  <aside id="krw-tags" class="widget widget-kratos-tags clearfix">
    <h4 class="widget-title"><i class="fa fa-tags"></i>标签聚合</h4>
      <div class="tag-clouds">
        <a href="/blog/tags/CORS/" style="font-size: 0.8em;">CORS</a> <a href="/blog/tags/Docker/" style="font-size: 0.8em;">Docker</a> <a href="/blog/tags/Dubbo/" style="font-size: 0.8em;">Dubbo</a> <a href="/blog/tags/ELK/" style="font-size: 0.8em;">ELK</a> <a href="/blog/tags/Elasticsearch/" style="font-size: 0.8em;">Elasticsearch</a> <a href="/blog/tags/Eureka/" style="font-size: 0.8em;">Eureka</a> <a href="/blog/tags/Feign/" style="font-size: 0.8em;">Feign</a> <a href="/blog/tags/Gateway/" style="font-size: 0.8em;">Gateway</a> <a href="/blog/tags/Idea/" style="font-size: 0.8em;">Idea</a> <a href="/blog/tags/Java/" style="font-size: 0.8em;">Java</a> <a href="/blog/tags/JavaWeb/" style="font-size: 0.8em;">JavaWeb</a> <a href="/blog/tags/Linux/" style="font-size: 0.8em;">Linux</a> <a href="/blog/tags/Maven/" style="font-size: 0.8em;">Maven</a> <a href="/blog/tags/MySQL/" style="font-size: 0.8em;">MySQL</a> <a href="/blog/tags/Mybatis/" style="font-size: 0.8em;">Mybatis</a> <a href="/blog/tags/Nacos/" style="font-size: 0.8em;">Nacos</a> <a href="/blog/tags/Pytest/" style="font-size: 0.8em;">Pytest</a> <a href="/blog/tags/Python/" style="font-size: 0.8em;">Python</a>
      </div>
  </aside>

            
                
  <aside id="krw-posts" class="widget widget-kratos-posts">
  <h4 class="widget-title"><i class="fa fa-file"></i>最新文章</h4>
  <div class="tab-content">
      <ul class="list-group">
        
        
          
          
            <a class="list-group-item" href="/blog/2024/04/07/Java%E6%97%A5%E5%BF%97%E6%A1%86%E6%9E%B6/Java%E6%97%A5%E5%BF%97%E6%A1%86%E6%9E%B6/"><i class="fa  fa-book"></i> 【Java】常见的日志框架</a>
            
          
        
          
          
            <a class="list-group-item" href="/blog/2024/04/06/SpringCloud/ElasticStack/Elasticsearch%E6%90%9C%E7%B4%A2%E8%AF%AD%E6%B3%95/"><i class="fa  fa-book"></i> 【ES】搜索语法</a>
            
          
        
          
          
            <a class="list-group-item" href="/blog/2024/04/03/SpringCloud/ElasticStack/Elasticsearch%E7%B4%A2%E5%BC%95%E9%85%8D%E7%BD%AE/"><i class="fa  fa-book"></i> 【ES】索引配置</a>
            
          
        
          
          
            <a class="list-group-item" href="/blog/2024/04/02/SpringCloud/ElasticStack/Kibana%E5%B8%B8%E7%94%A8%E5%8A%9F%E8%83%BD/"><i class="fa  fa-book"></i> 【Kibana】常用功能</a>
            
          
        
          
          
            <a class="list-group-item" href="/blog/2024/04/02/SpringCloud/ElasticStack/IK%E5%88%86%E8%AF%8D%E5%99%A8/"><i class="fa  fa-book"></i> 【IK 分词器】基本使用</a>
            
          
        
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
          
            
      </ul>
  </div>
  </aside>

            
    </div>
</section>
        
        </div>
    </div>
</div>
<footer>
    <div id="footer"  >
        <div class="container">
            <div class="row">
                <div class="col-md-6 col-md-offset-3 footer-list text-center">
                    <ul class="kratos-social-icons">
                        <!-- Keep for compatibility -->
                        
                        
                        
                        
                        
                        
                        
                        
                        
                        <!-- New links -->
                        
                    </ul>
                    <ul class="kratos-copyright">
                        <div>
                            <li>&copy; 2025 溪岚花的博客 版权所有.</li>
                            <li>本站已运行<span id="span_dt">Loading...</span></li>
                        </div>
                        <div>
                            <li>Theme <a href="https://github.com/Candinya/Kratos-Rebirth" target="_blank">Kratos:Rebirth</a></li>
                            <li>Site built with&nbsp;<i class="fa fa-heart throb" style="color:#d43f57"></i>&nbsp;by 林家隆.</li>
                        </div>
                        <div>
                            <li>Powered by <a href="https://hexo.io" target="_blank" rel="nofollow">Hexo</a></li>
                            <li>Hosted on <a href="https://gitee.com/" target="_blank">Gitee Pages</a></li>
                        </div>
                        <div>
                            
                            
                        </div>
                    </ul>
                </div>
            </div>
        </div>
        <div class="kr-tool text-center">
            <div class="tool">
                
                    <div class="box search-box">
                        <a href="/blog/search/">
                            <span class="fa fa-search"></span>
                        </a>
                    </div>
                
                
                    <div class="box theme-box" id="darkmode-switch">
                        <span class="fa fa-adjust"></span>
                    </div>
                
                
                
            </div>
            <div class="box gotop-box">
                <span class="fa fa-chevron-up"></span>
            </div>
        </div>
    </div>
</footer>
</div>
</div>

        <script defer src="/blog/vendors/bootstrap@3.3.4/dist/js/bootstrap.min.js"></script>
<script defer src="/blog/vendors/nprogress@0.2.0/nprogress.js"></script>
<script>
    if (!window.kr) {
        window.kr = {};
    }
    window.kr.notMobile = (!(navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)));
    window.kr.siteRoot = "/blog/";
</script>





    <script defer src="/blog/vendors/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js"></script>

<script defer src="/blog/js/kratosr.min.js"></script>
<script defer src="/blog/js/pjax.min.js"></script>



<!-- Extra support for third-party plguins  -->


    </body>
</html>